package org.gp.core.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.gp.core.entity.Comment;
import org.gp.core.entity.dto.ResponseDTO;
import org.gp.core.entity.enums.DTOCodeEnum;
import org.gp.core.mapper.CommentMapper;
import org.gp.core.service.ICommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 评论表  服务实现类
 * </p>
 *
 * @author LuoLiang
 * @since 2017-02-27
 */
@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Override
    public ResponseDTO commentActivity(Integer userId, Integer activityId, Integer score, String content) {
        ResponseDTO dto = new ResponseDTO();

        EntityWrapper<Comment> ew = new EntityWrapper<>();
        ew.where("user_id={0}", userId);
        ew.and("activity_id={0}", activityId);

        try {
            Comment comment = selectOne(ew);

            comment.setScore(score);
            comment.setContent(content);

            update(comment, ew);

            dto.setCode(DTOCodeEnum.SUCCESS.getDesc());
            dto.setResMsg("评论成功");
        } catch (Exception e) {
            log.error("评论业务异常：", e);
            dto.setCode(DTOCodeEnum.FAIL.getDesc());
            dto.setResMsg("评论失败");
        }

        return dto;
    }

    @Override
    public ResponseDTO<List<Comment>> getActivityCommentList(String activityId) {
        ResponseDTO<List<Comment>> dto = new ResponseDTO<>();

        EntityWrapper<Comment> ew = new EntityWrapper<>();
        ew.where("activity_id={0}", activityId);

        try {
            List<Comment> comments = selectList(ew);
            if (comments.isEmpty()) {
                Comment comment = new Comment();
                comment.setContent("暂无其他用户评论");
                comments.add(comment);
            }

            dto.setData(comments);
            dto.setCode(DTOCodeEnum.SUCCESS.getDesc());
        } catch (Exception e) {
            log.error("获取id为{}的活动评论列表异常：{}", activityId, e);
            dto.setCode(DTOCodeEnum.FAIL.getDesc());
        }

        return dto;
    }
}
